// Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

lazy val root = (project in file("."))
  .enablePlugins(PlayJava)
  .settings(commonSettings: _*)
  .settings(
    libraryDependencies += guice,
    PlayKeys.playInteractionMode := play.sbt.StaticPlayNonBlockingInteractionMode,
    InputKey[Unit]("makeRequestAndRecordResponseBody") := {
      val args = Def.spaceDelimited("<path> <dest> ...").parsed

      // <dest> is a relative path where the returned body will be stored/recorded
      val path :: dest :: Nil = args

      val destination = target.value / dest

      println(s"Firing request to $path...")
      val (status, body) = ScriptedTools.callUrl(path)
      println(s"Resource at $path returned HTTP $status")
      IO.write(destination, body)
    },
    InputKey[Unit]("checkLinesPartially") := {
      val args                  = Def.spaceDelimited("<source> <target>").parsed
      val source :: target :: _ = args
      ScriptedTools.checkLinesPartially(source, target)
    },
    InputKey[Unit]("checkLinesPartiallyScala2") := {
      if(scalaBinaryVersion.value != "3") {
        val args                  = Def.spaceDelimited("<source> <target>").parsed
        val source :: target :: _ = args
        ScriptedTools.checkLinesPartially(source, target)
      }
    },
    InputKey[Unit]("checkLinesPartiallyScala3") := {
      if(scalaBinaryVersion.value == "3") {
        val args                  = Def.spaceDelimited("<source> <target>").parsed
        val source :: target :: _ = args
        ScriptedTools.checkLinesPartially(source, target)
      }
    },
    InputKey[Unit]("checkLinesPartiallyNotContain") := {
      val args                  = Def.spaceDelimited("<source> <target>").parsed
      val source :: target :: _ = args
      ScriptedTools.checkLinesPartially(source, target, false)
    },
    InputKey[Unit]("verifyResourceContains") := {
      val args                         = Def.spaceDelimited("<path> <status> <words> ...").parsed
      val path :: status :: assertions = args
      ScriptedTools.verifyResourceContains(path, status.toInt, assertions)
    }
  )
  .dependsOn(`sub-project-inside`, `sub-project-outside`)
  .aggregate(`sub-project-inside`, `sub-project-outside`)

def commonSettings: Seq[Setting[_]] = Seq(
  scalaVersion := ScriptedTools.scalaVersionFromJavaProperties(),
  updateOptions := updateOptions.value.withLatestSnapshots(false),
  update / evictionWarningOptions ~= (_.withWarnTransitiveEvictions(false).withWarnDirectEvictions(false)),
  // This makes it possible to run tests on the output regardless of scala version
  crossPaths := false
)

lazy val `sub-project-inside` = (project in file("./modules/sub-project-inside"))
  .settings(commonSettings: _*)

lazy val `sub-project-outside` = ProjectRef(file("../dev-mode-compile-and-config-error-source-sub-project-outside"), "sub-project-outside")
